home *** CD-ROM | disk | FTP | other *** search
/ NOVA - For the NeXT Workstation / NOVA - For the NeXT Workstation.iso / SourceCode / MiniExamples / PerformanceTuning / VisibleView-01 / Timing.m < prev    next >
Encoding:
Text File  |  1991-09-18  |  2.6 KB  |  162 lines

  1. /* Timing.m */
  2.  
  3. #import "Timing.h"
  4. #import <stdio.h>
  5. #import <streams/streams.h>
  6. #import <dpsclient/wraps.h>
  7. #import <appkit/graphics.h>
  8.  
  9. @implementation Timing
  10.  
  11. +newWithTag:(int) aTag
  12. {
  13.     self = [super new];
  14.     tag = aTag;
  15.     [self reset];
  16.     return self;
  17. }
  18.  
  19. -enter:(int)wt
  20. {
  21.     if(wallTime = (wt==WALLTIME))
  22.     [self wallEnter];
  23.     else
  24.     [self psEnter];
  25.     return self;
  26. }
  27.  
  28. -wallEnter
  29. {
  30.     cumTimesEntered++;
  31.     NXPing();
  32.     gettimeofday(&realtime,&tzone);
  33.     synctime = realtime.tv_sec + realtime.tv_usec/1000000.0;
  34.     return self;
  35. }
  36.  
  37. -tare
  38. {
  39.     struct timezone tzone1;
  40.     struct timeval realtime1;
  41.     struct timeval realtime2;
  42.     NXPing();
  43.     gettimeofday(&realtime1,&tzone1);
  44.     NXPing();
  45.     gettimeofday(&realtime2,&tzone1);
  46.     tare = (-realtime1.tv_sec + realtime2.tv_sec)+ 
  47.             (-realtime1.tv_usec+realtime2.tv_usec)/1000000.0;
  48.     return self;
  49. }
  50.  
  51. -psEnter
  52. {
  53.     cumTimesEntered++;
  54.     PSusertime(&stime);
  55.     getrusage(RUSAGE_SELF,&rtime);
  56.     synctime = (rtime.ru_utime.tv_sec + rtime.ru_stime.tv_sec) +
  57.                 (rtime.ru_utime.tv_usec + rtime.ru_stime.tv_usec)/1000000.0;
  58.     return self;
  59. }
  60.  
  61. -wallLeave
  62. {
  63.     double eTime;
  64.     NXPing();
  65.     gettimeofday(&realtime,&tzone);
  66.     eTime = (- synctime + realtime.tv_sec + 
  67.                 realtime.tv_usec/1000000.0) -tare;
  68.     cumWallTime += eTime;
  69.     return self;
  70. }
  71.  
  72. -psLeave
  73. {
  74.     int et;
  75.     double appTime;
  76.     double psTime;
  77.  
  78.     getrusage(RUSAGE_SELF,&rtime);
  79.     PSusertime(&et);
  80.     psTime = ((et-stime)/1000.0);
  81.     cumPSTime += psTime;
  82.     appTime  = ((rtime.ru_utime.tv_sec + rtime.ru_stime.tv_sec) +
  83.                 (rtime.ru_utime.tv_usec + 
  84.                  rtime.ru_stime.tv_usec)/1000000.0) -synctime;
  85.     cumAppTime += appTime;
  86.     return self;
  87. }
  88.  
  89. -leave
  90. {
  91.     if(wallTime)
  92.     [self wallLeave];
  93.     else
  94.     [self psLeave];
  95.     return self;
  96. }
  97.  
  98. -reset
  99. {
  100.     cumAppTime = 0.0;
  101.     cumPSTime = 0.0;
  102.     cumWallTime = 0.0;
  103.     cumTimesEntered = 0;
  104.     return self;
  105. }
  106.  
  107. -avgElapsedTime
  108. {
  109.     if(wallTime)
  110.         avgWallTime = (cumWallTime/(double)cumTimesEntered);
  111.     else{
  112.         avgAppTime = (cumAppTime/(double)cumTimesEntered) ;
  113.         avgPSTime = (cumPSTime/(double)cumTimesEntered);
  114.     }
  115.     return self;
  116. }
  117.  
  118. -(double) cumWallTime
  119. {
  120.     if(wallTime ==WALLTIME)
  121.     return cumWallTime;
  122.     else
  123.     return -1.0;
  124. }
  125.  
  126. -(double) cumAppTime;
  127. {
  128.     if(wallTime ==PSTIME)
  129.     return cumAppTime;
  130.     else
  131.     return -1.0;
  132. }
  133.  
  134. -(double) cumPSTime;
  135. {
  136.     if(wallTime ==PSTIME)
  137.     return cumPSTime;
  138.     else
  139.     return -1.0;
  140. }
  141.  
  142. -summary:sender
  143. {
  144.     if(wallTime) {
  145.     fprintf(stderr,"Timer %d : entered %d trials TotalWall Time  %lf 
  146.                 \n",
  147.     tag,cumTimesEntered, cumWallTime);
  148.     }
  149.     else {
  150.     fprintf(stderr,"Timer %d : %d trials App: %lf  Server: %lf  
  151.                 Percent Server: %lf Total: %lf\n\00", 
  152.                 tag,cumTimesEntered,cumAppTime,cumPSTime,
  153.                 cumPSTime/(cumAppTime+cumPSTime),
  154.                 cumAppTime+cumPSTime);
  155.     }
  156.     
  157.     return self;
  158. }
  159.  
  160. @end
  161.  
  162.